package meterMonitor.controller;

import meterMonitor.biz.UserAuthBiz;
import meterMonitor.biz.UserBiz;
import meterMonitor.consts.ResponseCode;
import meterMonitor.framework.auth.AuthorityType;
import meterMonitor.framework.auth.FireAuthority;
import meterMonitor.framework.context.CurrentUser;
import meterMonitor.model.Auth;
import meterMonitor.model.User;
import meterMonitor.util.StringUtils;
import meterMonitor.vo.BaseResponseData;
import meterMonitor.vo.UserExtend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/2/23 0023.
 * 创建用户
 */
@RequestMapping("userManger")
@RestController
public class CreateUserController extends BaseController {
    @Autowired
    UserAuthBiz userAuthBiz;
    @Autowired
    UserBiz userBiz;

    /**
     * 用户管理页面
     *
     * @param mv
     * @return
     */
    @RequestMapping("userManger.do")
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public ModelAndView index(ModelAndView mv) {
        return mv;
    }

    /**
     * 查询所有用户以及用户所对应的权限
     */
    @RequestMapping("selectAllUser.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public List<UserExtend> selectAllUser() {
        try {
            List<User> list = userBiz.selectAllUser();
            List<UserExtend> listEx = new ArrayList<>();
            for (User user : list) {
                UserExtend userExtend = new UserExtend();
                userExtend.setLoginName(user.getLoginName());
                userExtend.setPassWord(user.getPassword());
                userExtend.setPhoneNo(user.getRemark());
                userExtend.setAuths(user.getCompanyName());
                listEx.add(userExtend);
            }

            return listEx;
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 添加用户
     * 向用户表添加记录
     * 向用户权限表添加记录
     *
     * @param
     * @return currentUser
     */
    @RequestMapping("insert.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public BaseResponseData insertUser(UserExtend userExtend) {
        CurrentUser currentUser = new CurrentUser();
        currentUser.setAddedTime(new Date());
        currentUser.setLastModifyTime(new Date());
        currentUser.setRemark(userExtend.getPhoneNo());
        currentUser.setLoginName(userExtend.getLoginName());
        currentUser.setPassword(userExtend.getPassWord());
        String str = userExtend.getAuths();
        if (StringUtils.isNotBlank(str)) {
            String[] arr = str.split(",");
            List<String> list = new ArrayList<>();
            for (int i = 0; i < arr.length; i++) {
                list.add(arr[i]);
            }
            currentUser.setUserAuthList(list);
        }


        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            List<String> listLogName = userBiz.getLoginNames();
            for (String loginName : listLogName) {
                if (userExtend.getLoginName().equals(loginName)) {
                    baseResponseData.setMessage("该用户名已经被注册，请重新输入！");
                    baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
                    return baseResponseData;
                }
            }
            userAuthBiz.insertUserAuth(currentUser);
            userBiz.insetUser(currentUser);

            baseResponseData.setMessage("添加用户成功！");
            baseResponseData.setCode(ResponseCode.SUCCESS);
            return baseResponseData;
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            baseResponseData.setMessage("添加用户失败");
        }

        return baseResponseData;
    }

    /**
     * 查询用户的所有权限
     *
     * @return
     */
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    @RequestMapping("getAuth.do")
    @ResponseBody
    public List<Auth> selectAuth() {
        try {
            return userAuthBiz.getAuths();
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
        }

        return null;
    }

    /**
     * 删除用户
     */
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    @RequestMapping("deleteUser.do")
    @ResponseBody
    public BaseResponseData deleteUser(User user) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            userBiz.deleteUser(user);
            baseResponseData.setCode(ResponseCode.SUCCESS);
            baseResponseData.setMessage("删除用户成功！");
            return baseResponseData;
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("删除用户失败！");
        }
        return baseResponseData;
    }

    /**
     * 编辑用户
     */
    @RequestMapping("editUser.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public BaseResponseData editUser(UserExtend userExtend) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            userBiz.setUser(userExtend);
            baseResponseData.setMessage("编辑用户成功！");
            baseResponseData.setCode(ResponseCode.SUCCESS);
            return baseResponseData;
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("编辑用户失败！");
        }
        return baseResponseData;
    }

}
